const BaseController = require('./base_controller.js');
const CouponVerificationService = require('../service/coupon_verification_service.js');

class CouponVerificationController extends BaseController {

  /**
   * 验证券码
   */
  async verifyCoupon() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      couponCode: 'required|string|min:1|max:50|name=券码',
      platform: 'required|string|in:meituan,douyin|name=平台',
      orderAmount: 'required|int|min:1|name=订单金额',
      storeId: 'required|string|name=门店ID',
    };

    let input = this.validateData(rules);

    try {
      const service = new CouponVerificationService();
      const result = await service.verifyCoupon(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 获取券码列表
   */
  async getCouponList() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      page: 'int|min:1|default=1|name=页码',
      size: 'int|min:1|max:50|default=20|name=每页数量',
      platform: 'string|name=平台',
      status: 'int|name=券码状态',
      search: 'string|max:50|name=搜索关键词',
    };

    let input = this.validateData(rules);

    try {
      const service = new CouponVerificationService();
      const result = await service.getCouponList(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 获取验券记录
   */
  async getVerificationLogs() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      page: 'int|min:1|default=1|name=页码',
      size: 'int|min:1|max:50|default=20|name=每页数量',
      couponId: 'string|name=券码ID',
      platform: 'string|name=平台',
      dateRange: 'string|name=日期范围',
    };

    let input = this.validateData(rules);

    try {
      const service = new CouponVerificationService();
      const result = await service.getVerificationLogs(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }
}

module.exports = CouponVerificationController;
